package com.gl.controller.user;

import com.gl.pojo.dto.*;
import com.gl.pojo.Result;
import com.gl.pojo.entity.User;
import com.gl.pojo.vo.UserVo;
import com.gl.service.*;
import com.gl.utils.CurrentHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/users")
//@CrossOrigin("*")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private MetricsService metricsService;
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private MeetingService meetingService;

    /**
     * 发送验证码
     * @param email 用户要注册的邮箱
     * @return 响应
     */
    @PostMapping("/captcha")
    public Result captcha(@RequestParam() String email) {
        userService.captcha(email);
        return Result.success();
    }

    /**
     * 用户账号注册
     * @param request 用户发送的注册请求
     * @return 响应
     */
    @PostMapping("/register")
    @Transactional
    public Result register(UserRegisterRequest request) {
        UserRegisterResponse response = userService.register(request);
        metricsService.insertUser();
        return Result.success(response);
    }

    /**
     * 登录
     * @param email 用户邮箱
     * @param password 用户密码
     * @return 响应
     */
    @PostMapping("/login")
    public Result login(@RequestParam() String email, @RequestParam() String password) {
        UserLoginResponse response = userService.login(email, password);
        metricsService.online();
        return Result.success(response);
    }

    /**
     * 登出
     * @return 响应
     */
    @PostMapping("/logout")
    public Result userLogout(){
        metricsService.offline();
        return Result.success();
    }

    /**
     * 获取用户信息
     * @return 响应
     */
    @GetMapping("/info")
    public Result info() {
        String userId = CurrentHolder.getCurrentId();
        if (userId == null)return Result.error("请先登录");
        User user = userService.selectById(userId);
        long l = meetingService.countByUserId(userId);
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user, userVo);
        userVo.setMeetingCount(l);
        return Result.success(userVo);
    }

    /**
     * 注销用户
     * @return 响应
     */
    @PostMapping("/delete")
    public Result deleteUser() {
        String userId = CurrentHolder.getCurrentId();
        userService.deleteById(userId);
        return Result.success();
    }

    /**
     * 更新用户名
     * @param username 新的用户名
     * @return 响应
     */
    @PostMapping("/update/username")
    public Result updateUsername(String username) {
        User user = new User();
        user.setId(CurrentHolder.getCurrentId());
        user.setUsername(username);
        userService.updateUser(user);
        return Result.success();
    }

    /**
     * 修改头像
     * @param file 头像文件
     * @return 响应
     */
    @PostMapping("/update/avatar")
    public Result updateAvatar(@RequestParam("avatar") MultipartFile file) {
        userService.updateAvatar(file);
        return Result.success();
    }

    /**
     * 验证密码
     */
    @PostMapping("/identify/password")
    public Result identifyPassword(User user) {
        if (userService.identifyPassword(user)) return Result.success("密码正确");
        return Result.error("密码错误");
    }

    /**
     * 验证邮箱
     */
    @PostMapping("/identify/email")
    public Result identifyEmail(String captcha) {
        if (userService.identifyCaptcha(captcha)) return Result.success("验证码正确");
        return Result.error("验证码错误");
    }

    /**
     * 修改密码
     */
    @PostMapping("/update/password")
    public Result updatePassword(User user) {
        user.setId(CurrentHolder.getCurrentId());
        userService.updateUser(user);
        return Result.success();
    }

    @PostMapping("/permissions/apply")
    public Result applyPermissions(@RequestParam String reason) {
        return permissionService.applyPermissions(reason);
    }
}